home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / tvtoys04.zip / MODEDLG.PAS < prev    next >
Pascal/Delphi Source File  |  1993-12-15  |  12KB  |  400 lines

  1. (***************************************************************************
  2.   ModeDialog unit
  3.   A dialog displaying available video modes, supporting routines
  4.   PJB August 30, 1993, Internet mail to d91-pbr@nada.kth.se
  5.   Copyright PJB 1993, All Rights Reserved.
  6.   Free source, use at your own risk.
  7.   If modified, please state so if you pass this around.
  8.  
  9.   If you want to omit certain video modes from the list, change the
  10.   AddMode procedure to include a test (e.g. if Columns<80 then Exit...)
  11.  
  12.   Turbo Vision works in 40 columns, but the SelectVideoMode dialog does
  13.   not (it is too wide, selecting Preview will shrink the dialog).
  14.  
  15.   You can overlay this unit and put TSelectVideoModeDialog in a
  16.   resource file. Here is what to do with a resource file:
  17.  
  18.     SetupVideoList;
  19.     SelectVideoMode(PSelectVideoModeDialog(RezFile.Get('VideoModeDialog')));
  20.  
  21.  
  22.   See VIDEOTST.PAS for a demonstration of this unit.
  23.  
  24. ***************************************************************************)
  25. unit ModeDlg;
  26.  
  27. {$I toyCfg}
  28.  
  29. {$B-,O+,Q-,T-,X+}
  30.  
  31. interface
  32.  
  33.   uses
  34.     App, Dialogs, Drivers, Objects, Memory, MsgBox, Views,
  35.     toyPrefs, {$I hcFile}
  36.     TVVideo, Video;
  37.  
  38.   type
  39.     PSelectVideoModeDialog = ^TSelectVideoModeDialog;
  40.     TSelectVideoModeDialog =
  41.       object (TDialog)
  42.         VideoListBox : PListBox;
  43.         constructor Init;
  44.         constructor Load(var S:TStream);
  45.         procedure HandleEvent(var Event:TEvent); virtual;
  46.         procedure Store(var S:TStream);
  47.       end;
  48.  
  49.   procedure StoreVideoModes(var S:TStream);
  50.   procedure LoadVideoModes(var S:TStream);
  51.  
  52.   procedure Delay(Ticks:word);
  53.  
  54.   procedure SetupVideoList;
  55.   function  HasToScan:Boolean;
  56.   procedure SelectVideoModeDialog;
  57.   procedure SelectVideoMode(P:PSelectVideoModeDialog);
  58.  
  59.   var
  60.     (* SelectVideoModeDialog GetData/SetData operates on this *)
  61.     VideoModeDataRec :
  62.       record
  63.         VideoListBox : TListboxRec;
  64.       end;
  65.  
  66.     (* The ModeList array contains the actual video modes
  67.        corresponding to the entries in the VideoList listbox *)
  68.     ModeList  : array [0..MaxVideoModes] of Word;
  69.  
  70.  
  71.  {$IFDEF StoreModeData}
  72.   type
  73.     ModeDataRec =
  74.       record
  75.         Columns    : Byte;
  76.         Rows       : Byte;
  77.         CharHeight : Byte;
  78.         Color      : Boolean;
  79.       end;
  80.  
  81.   var
  82.     (* The ModeDataList array contains each video mode's
  83.        width, height and character size for matching purposes *)
  84.     ModeDataList : array [0..MaxVideoModes] of ModeDataRec;
  85.  
  86.   function FindSimilarVideoMode(Columns, Rows:Byte; Color:Boolean):Word;
  87.  {$ENDIF}
  88.  
  89. (***************************************************************************
  90. ***************************************************************************)
  91. implementation
  92.  
  93.  
  94.   var
  95.     (* AddMode adds new lines of video mode information to VideoList *)
  96.     VideoList : PStringCollection;
  97.  
  98.  
  99.   (*******************************************************************
  100.     These routines save mode information on a stream. They are meant
  101.     to be used with an init or configuration file
  102.   *******************************************************************)
  103.   procedure StoreVideoModes;
  104.   begin
  105.     S.Put(VideoList);
  106.     S.Write(ModeList, SizeOf(ModeList));
  107.    {$IFDEF StoreModeData}
  108.     S.Write(ModeDataList, SizeOf(ModeDataList));
  109.    {$ENDIF}
  110.   end;
  111.  
  112.   procedure LoadVideoModes;
  113.   begin
  114.     VideoList:=PStringCollection(S.Get);
  115.     S.Read(ModeList, SizeOf(ModeList));
  116.    {$IFDEF StoreModeData}
  117.     S.Read(ModeDataList, SizeOf(ModeDataList));
  118.    {$ENDIF}
  119.   end;
  120.  
  121.  
  122.  
  123.   (*******************************************************************
  124.     Delay for Ticks 18ths of a second, calling Idle
  125.   *******************************************************************)
  126.   procedure Delay(Ticks:word);
  127.     var
  128.       Finish : Word;
  129.   begin
  130.     Finish:=MemW[Seg0040:$6C]+Ticks;
  131.     while Finish-MemW[Seg0040:$6C]<=Ticks do
  132.       Application^.Idle;
  133.   end;
  134.  
  135.  
  136.  {$IFDEF StoreModeData}
  137.   (*******************************************************************
  138.     Simple example of how to find a reasonably similar video mode
  139.     Tries to weigh Width and Height differently.
  140.   *******************************************************************)
  141.   function FindSimilarVideoMode(Columns, Rows:Byte; Color:Boolean):Word;
  142.     var
  143.       Diff    : Word;
  144.       OldDiff : Word;
  145.       i       : Integer;
  146.   begin
  147.     FindSimilarVideoMode:=ScreenMode;
  148.     OldDiff:=999;
  149.  
  150.     for i:=0 to VideoList^.Count-1 do
  151.     begin
  152.       Diff:=Abs(ModeDataList[i].Rows-Rows)+
  153.             Abs(ModeDataList[i].Columns-Columns) div 2+
  154.             20*Ord(ModeDataList[i].Color<>Color);
  155.       if Diff<OldDiff then
  156.       begin
  157.         OldDiff:=Diff;
  158.         FindSimilarVideoMode:=ModeList[i];
  159.       end;
  160.     end;
  161.   end;
  162.  {$ENDIF}
  163.  
  164.  
  165.   (*******************************************************************
  166.     This procedure will be called by Video.ScanEVGAModes with
  167.     new mode information.
  168.   *******************************************************************)
  169.   procedure AddMode(Mode, Rows, Columns, CharHeight:Word; Color:boolean); far;
  170.     const
  171.       ColorStr : string[5] = 'color';
  172.       MonoStr  : string[4] = 'mono';
  173.       BWStr    : string[3] = 'b/w';
  174.     var
  175.       Params : array [0..4] of Longint;
  176.       Line   : String;
  177.       i      : Integer;
  178.   begin
  179.     if (Columns>=80) and (VideoList^.Count<=MaxVideoModes) then
  180.     begin
  181.       Params[0]:=Mode;
  182.       Params[1]:=Columns;
  183.       Params[2]:=Rows;
  184.       Params[3]:=CharHeight;
  185.  
  186.       if Mode=smBW80 then
  187.         Params[4]:=LongInt(@BWStr)
  188.       else
  189.         if Color then
  190.           Params[4]:=LongInt(@ColorStr)
  191.         else
  192.           Params[4]:=LongInt(@MonoStr);
  193.  
  194.       FormatStr(Line, '%3xh  %3dx%-2d  %2dp  %s', Params);
  195.  
  196.       i:=VideoList^.Count;
  197.       ModeList[i]:=Mode;
  198.  
  199.      {$IFDEF StoreModeData}
  200.       ModeDataList[i].Columns:=Columns;
  201.       ModeDataList[i].Rows:=Rows;
  202.       ModeDataList[i].CharHeight:=CharHeight;
  203.       ModeDataList[i].Color:=Color;
  204.      {$ENDIF}
  205.  
  206.       VideoList^.Insert(NewStr(Line));
  207.     end;
  208.   end;
  209.  
  210.  
  211.   (*******************************************************************
  212.     Scan for video modes and add to VideoList
  213.   *******************************************************************)
  214.   procedure SetupVideoList;
  215.   begin
  216.     if VideoList=Nil then    (* Check for previous list... *)
  217.     begin
  218.       New(VideoList, Init(20,10));
  219.  
  220.      {$IFDEF VesaSupport}
  221.       if VESA.VesaScanningPossible then
  222.       begin
  223.         (************************************************************
  224.           Add standard modes if necessary, Marek Bojarski's idea
  225.         ************************************************************)
  226.         if not VESA.StandardInfoAvailable then
  227.         begin
  228.           HideMouse;
  229.           ScanEVGAModes(0, StandardTextModes, AddMode);
  230.           SetSpecialScreenMode(ScreenMode);
  231.           ShowMouse;
  232.         end;
  233.         VESA.ScanVesaModes(AddMode)
  234.       end
  235.       else
  236.      {$ENDIF}
  237.       begin
  238.         HideMouse;
  239.  
  240.         ScanEVGAModes(0, VGAModes, AddMode);
  241.  
  242.        {$IFDEF VesaSupport}   (* If not VesaScanningPossible *)
  243.         if VESA.VesaVersion<>0 then
  244.           ScanEVGAModes($100, VESAModes, AddMode);
  245.        {$ENDIF}
  246.  
  247.         (* Restore Turbo Vision screen *)
  248.         SetSpecialScreenMode(ScreenMode);
  249.         ShowMouse;
  250.       end;
  251.     end;
  252.     VideoModeDataRec.VideoListBox.List:=VideoList;
  253.   end;
  254.  
  255.  
  256.   (*******************************************************************
  257.     Return True if there is no previous list of video modes
  258.   *******************************************************************)
  259.   function HasToScan:Boolean;
  260.   begin
  261.     HasToScan:=VideoList=Nil;
  262.   end;
  263.  
  264.  
  265.   (*******************************************************************
  266.     Let the user select a video mode
  267.   *******************************************************************)
  268.   procedure SelectVideoModeDialog;
  269.   begin
  270.     SelectVideoMode(New(PSelectVideoModeDialog, Init));
  271.   end;
  272.  
  273.  
  274.   (*******************************************************************
  275.     Dialog already created, now execute it
  276.   *******************************************************************)
  277.   procedure SelectVideoMode(P:PSelectVideoModeDialog);
  278.     var
  279.       i : Integer;
  280.   begin
  281.     for i:=0 to VideoList^.Count-1 do
  282.       if ModeList[i]=ScreenMode then
  283.         VideoModeDataRec.VideoListbox.Selection:=i;
  284.  
  285.     if Application^.ExecuteDialog(P, @VideoModeDataRec)=cmOK then
  286.       if VideoList^.Count>0 then
  287.         SetSpecialScreenMode(ModeList[VideoModeDataRec.VideoListBox.Selection]);
  288.   end;
  289.  
  290.  
  291.  
  292. (***************************************************************************
  293.   Here comes the dialog object
  294. ***************************************************************************)
  295.  
  296.   const                  (* Command number irrelevant since local *)
  297.     cmPreview = 1000;
  298.     cmRescan  = 1001;
  299.  
  300.  
  301.   (*******************************************************************
  302.     This procedure generated by Dialog Design 4.0 available by anonymous
  303.     ftp to garbo.uwasa.fi  /pc/turbovis. Thanks to David Baldwin
  304.   *******************************************************************)
  305.   constructor TSelectVideoModeDialog.Init;
  306.     var
  307.       R : TRect;
  308.       Control : PView;
  309.   begin
  310.     R.Assign(14, 3, 66, 20);
  311.     inherited Init(R, 'Select Video Mode');
  312.     Options := Options or ofCentered;
  313.  
  314.     R.Assign(32, 3, 33, 15);
  315.     Control := New(PScrollBar, Init(R));
  316.     Insert(Control);
  317.  
  318.     R.Assign(5, 3, 32, 15);
  319.     VideoListBox := New(PListBox, Init(R, 1, PScrollbar(Control)));
  320.     VideoListBox^.HelpCtx := hctoyVideoListBox;
  321.     Insert(VideoListBox);
  322.  
  323.     R.Assign(4, 2, 16, 3);
  324.     Insert(New(PLabel, Init(R, '~V~ideo modes', VideoListBox)));
  325.  
  326.     R.Assign(37, 3, 48, 5);
  327.     Control := New(PButton, Init(R, '~P~review', cmPreview, bfDefault));
  328.     Control^.HelpCtx := hctoyVideoPreview;
  329.     Insert(Control);
  330.  
  331.     R.Assign(37, 6, 48, 8);
  332.     Control := New(PButton, Init(R, 'O~K~', cmOK, bfNormal));
  333.     Control^.HelpCtx := hcOK;
  334.     Insert(Control);
  335.  
  336.     R.Assign(37, 8, 48, 10);
  337.     Control := New(PButton, Init(R, 'Cancel', cmCancel, bfNormal));
  338.     Control^.HelpCtx := hcCancel;
  339.     Insert(Control);
  340.  
  341.     R.Assign(37, 11, 48, 13);
  342.     Control := New(PButton, Init(R, '~R~escan', cmRescan, bfNormal));
  343.     Control^.HelpCtx := hctoyVideoRescan;
  344.     Insert(Control);
  345.  
  346.     R.Assign(37, 14, 48, 16);
  347.     Control := New(PButton, Init(R, 'Help', cmHelp, bfNormal));
  348.     Control^.HelpCtx := hctoyVideoDialogHelp;
  349.     Insert(Control);
  350.  
  351.     SelectNext(False);
  352.   end;
  353.  
  354.   constructor TSelectVideoModeDialog.Load;
  355.   begin
  356.     inherited Load(S);
  357.     GetSubViewPtr(S, VideoListBox);
  358.   end;
  359.  
  360.   procedure TSelectVideoModeDialog.HandleEvent;
  361.     var
  362.       OldMode : Word;
  363.   begin
  364.     inherited HandleEvent(Event);
  365.     if (Event.What and evMessage<>0) then
  366.     begin
  367.       case Event.Command of
  368.         cmListItemSelected,      (* Mouse double clicked in list *)
  369.         cmPreview:
  370.           begin
  371.             OldMode:=ScreenMode;
  372.             SetSpecialScreenMode(ModeList[VideoListBox^.Focused]);
  373.             Delay(PreviewTime);
  374.             SetSpecialScreenMode(OldMode);
  375.           end;
  376.         cmRescan:
  377.           begin
  378.             VideoList:=Nil;
  379.             SetupVideoList;
  380.             VideoListBox^.NewList(VideoList);
  381.           end;
  382.         else
  383.           Exit;
  384.       end;
  385.       ClearEvent(Event);
  386.     end;
  387.   end;
  388.  
  389.   procedure TSelectVideoModeDialog.Store;
  390.   begin
  391.     inherited Store(S);
  392.     PutSubViewPtr(S, VideoListBox);
  393.   end;
  394.  
  395.  
  396.     (*******************************************************************
  397.     *******************************************************************)
  398.  
  399. end.
  400.